//
//  GoodsDetailViewController.m
//  Wedding fantastic
//
//  Created by dllo on 15/7/1.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "GoodsDetailViewController.h"
#import "AFNetworking.h"
#import "UIImageView+WebCache.h"
#import "RecommendModel.h"
#import "DetailGoodsCell.h"
#import "RecommendModel.h"
#import "MBProgressHUD.h"
#import "RecommendViewController.h"
#import "BuyViewController.h"

#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height

@interface GoodsDetailViewController ()<UIAlertViewDelegate, UICollectionViewDataSource, UICollectionViewDelegate>
// 最下面的
@property (nonatomic, retain) UIScrollView *backScrollView;
// 商品详情名字label
@property (nonatomic, retain) UILabel *introduceLabel;
//
@property (nonatomic, retain) UICollectionView *goodsCollectionView;
// 接收model数组
@property (nonatomic, retain) NSMutableArray *picArr;

//
@property (nonatomic, copy)NSString *urlStr;

//
@property (nonatomic, retain) MBProgressHUD *HUD;

@end

@implementation GoodsDetailViewController
- (void)dealloc
{
    [_sendModel release];
    [_backScrollView release];
    [_introduceLabel release];
    [_goodsCollectionView release];
    [_picArr release];
    [_HUD release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithRed:241 / 255.0 green:241 / 255.0 blue:240 / 255.0 alpha:1];
    self.navigationItem.title = @"商品详情";
    // 设置标题颜色
    self.navigationController.navigationBar.tintColor = [UIColor brownColor];
    // 字体颜色
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor brownColor],NSForegroundColorAttributeName,nil]];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back.png" ] style:UIBarButtonItemStyleDone target:self action:@selector(leftBack:)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"shoucang"] style:UIBarButtonItemStyleDone target:self action:@selector(rightClick:)];
    
    
    self.backScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64 -49)];
    self.backScrollView.backgroundColor = [UIColor colorWithRed:223 / 255.0 green:223 / 255.0 blue:223 / 255.0 alpha:1];
    [self.view addSubview:self.backScrollView];
    //
    self.backScrollView.showsVerticalScrollIndicator = FALSE;
    // 按页滚动
    self.backScrollView.pagingEnabled = NO;
    // 滚动范围
    self.backScrollView.contentSize = CGSizeMake(0, HEIGHT *1);
    
    
    // 详情View
    UIView *detailView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 430 * HEIGHT / 667)];
    detailView.backgroundColor = [UIColor whiteColor];
    [self.backScrollView addSubview:detailView];
    [detailView release];
    
    // 详情图片
    UIImageView *detailPicImage = [[UIImageView alloc] initWithFrame:CGRectMake(20 * WIDTH / 375, 10 * HEIGHT / 667, 335 * WIDTH / 375, 330 * HEIGHT / 667)];
    detailPicImage.backgroundColor = [UIColor cyanColor];
    [detailPicImage sd_setImageWithURL:[NSURL URLWithString:self.sendModel.photo_path]];
    [detailView addSubview:detailPicImage];
    [detailPicImage release];
    
    // 详情介绍label
    self.introduceLabel = [[UILabel alloc] initWithFrame:CGRectMake(20 * WIDTH / 375, 355 * HEIGHT / 667, 335 * WIDTH / 375, 40 * HEIGHT / 667)];
    self.introduceLabel.numberOfLines = 0;
    self.introduceLabel.font = [UIFont systemFontOfSize:14];
    [detailView addSubview:self.introduceLabel];
    [self.introduceLabel release];
    
    // 详情价钱label
    UILabel *detailPriceLabel = [[UILabel alloc] initWithFrame:CGRectMake(120 * WIDTH / 375, 390 * HEIGHT / 667, 150 * WIDTH / 375, 35 * HEIGHT / 667)];
    // 对齐方式
    detailPriceLabel.textAlignment = UITextAlignmentCenter;
    detailPriceLabel.text = [NSString stringWithFormat:@"￥ %@", [self.sendModel.price stringValue]];
    detailPriceLabel.textColor = [UIColor redColor];
    [detailView addSubview:detailPriceLabel];
    [detailPriceLabel release];
    
    // 购买button
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(245 * WIDTH / 375, 393 * HEIGHT / 667, 60 * WIDTH / 377, 30 * HEIGHT / 667);
    [button setTitle:@"去购买" forState:UIControlStateNormal];
    button.backgroundColor = [UIColor redColor];
    button.alpha = 0.7;
    button.layer.cornerRadius = 5;
    [button setTintColor:[UIColor whiteColor]];
    [detailView addSubview:button];
    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    // 精品推荐view
    UIView *qualityGoodsView = [[UIView alloc] initWithFrame:CGRectMake(0, 445 * HEIGHT / 667, WIDTH, 200 * HEIGHT /667)];
    qualityGoodsView.backgroundColor = [UIColor whiteColor];
    [self.backScrollView addSubview:qualityGoodsView];
    [qualityGoodsView release];
    
    // 精品推荐label
    UILabel *goodsLabel = [[UILabel alloc] initWithFrame:CGRectMake(5 * WIDTH / 375, 5 * HEIGHT / 667, WIDTH - 10, 20 * HEIGHT /667)];
    goodsLabel.text = @"精品推荐";
    goodsLabel.font = [UIFont systemFontOfSize:14];
    [qualityGoodsView addSubview:goodsLabel];
    [goodsLabel release];
    
    // 精品推荐collectionView
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(WIDTH / 4 + 10, 80 * HEIGHT / 667);
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 10, 0, 10);
    
    self.goodsCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 30 * HEIGHT / 667, WIDTH, 200 * HEIGHT / 667) collectionViewLayout:flowLayout];
    self.goodsCollectionView.backgroundColor = [UIColor whiteColor];
    self.goodsCollectionView.delegate = self;
    self.goodsCollectionView.dataSource = self;
    [qualityGoodsView addSubview:self.goodsCollectionView];
    [_goodsCollectionView release];
    
    // 注册
    [self.goodsCollectionView registerClass:[DetailGoodsCell class] forCellWithReuseIdentifier:@"reuse"];
    
    [self createData];
    [self createGoodsData];
}

- (void)rightClick:(UIButton *)button
{
    
}

// 购买按钮
- (void)buttonClick:(UIButton *)button
{
    BuyViewController *buyVC = [[BuyViewController alloc] init];
    buyVC.sendUrl = self.urlStr;
    [self.navigationController pushViewController:buyVC animated:YES];
    [buyVC release];
}

// 导航控制器返回按钮点击方法
- (void)leftBack:(UIButton *)button
{
    [self.navigationController popViewControllerAnimated:YES];
}

// collectionview方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.picArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    DetailGoodsCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    RecommendModel *model = self.picArr[indexPath.row];
    cell.backgroundColor = [UIColor whiteColor];
    [cell.picImageView sd_setImageWithURL:[NSURL URLWithString:model.photo_path]];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    GoodsDetailViewController *GoodsVC = [[GoodsDetailViewController alloc] init];
    GoodsVC.sendModel = self.picArr[indexPath.row];
    [self.navigationController pushViewController:GoodsVC animated:YES];
    [GoodsVC release];
}

// 解析物品名字
- (void)createData
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:[NSString stringWithFormat:@"http://hunliji.com/products/%@.json?user_id=984695", self.sendModel.bId] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = responseObject;
        NSDictionary *productDic = dic[@"product"];
        NSString *str = productDic[@"introduce"];
        self.urlStr = productDic[@"buy_url"];
        self.introduceLabel.text = str;
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}

// 解析精品推荐图片
- (void)createGoodsData
{
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self.HUD setDimBackground:YES];
    self.HUD.labelText = @"努力加载中...";
    [self.HUD show:YES];
    
    self.picArr = [NSMutableArray array];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:@"http://hunliji.com/productRecommend.json?count=6" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = responseObject;
        NSArray *productsArr = dic[@"products"];
        for (NSDictionary *tempDic in productsArr) {
            RecommendModel *model = [[RecommendModel alloc] init];
            [model setValuesForKeysWithDictionary:tempDic];
            [self.picArr addObject:model];
            [model release];
        }
        [self.HUD hide:YES];
        [self.goodsCollectionView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"可怜的~" message:@"你的网络有问题哦~~%>_<%" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
        [alert show];
        [alert release];
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
